<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="exportFile">导出</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="exportFileAll">导出全部数据</el-button>
        </el-form-item>

        <el-form-item>
        <el-upload
            class="upload-demo"
            multiple=""
            method="post"
            action="http://localhost:8080/course/updown"
            style="margin-left: 10px"
            accept=".xlsx,.xls"
            :show-file-list="false"
            :on-success="success"
            name="files"
        >
          <el-button type="primary">选取EXCEL文件导入</el-button>
        </el-upload>
        </el-form-item>

      </el-form>
    </el-col>

    <!--列表v-loading="listLoading"
    :data="tableDatas" -表格数据
    v-loading="listLoading" -> 加载圈
    @selection-change="handleSelectionChange" -> 批量删除
    highlight-current-row  -> 选择一行数据，变亮
    -->

    <el-table :data="tableDatas" v-loading="listLoading"
              @selection-change="handleSelectionChange" highlight-current-row
              style="width: 100%;" :row-key="SpreadPages">
      <!--多选框-->
      <el-table-column type="selection" width="55" :reserve-selection="true">
      </el-table-column>
      <el-table-column type="index" width="70" label="序号">
      </el-table-column>

      <el-table-column prop="lectureTime" label="授课时间" width="120">
      </el-table-column>


      <el-table-column prop="week" label="星期时间" width="120">
      </el-table-column>

      <el-table-column prop="content" label="授课内容" width="260">
      </el-table-column>

      <el-table-column prop="teacher" label="授课讲师" width="200">
      </el-table-column>

      <el-table-column prop="remark" label="备注" width="200">
      </el-table-column>

      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" :disabled="this.sels.length === 0" @click="handlePatchDel">批量删除</el-button>
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange" :current-page="currentPage"
                     :page-sizes="[5, 10, 15]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                     background
                     :total="totals" style="float:right;">
      </el-pagination>
    </el-col>


    <!--添加或修改界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">

        <el-form-item label="授课内容" prop="content">
          <el-input v-model="saveForm.content" auto-complete="off"></el-input>
        </el-form-item>


        <el-form-item label="授课讲师" prop="teacher">
          <el-input v-model="saveForm.teacher" auto-complete="off"></el-input>
        </el-form-item>


        <el-form-item label="授课时间">
          <template>
            <div class="block">
              <el-date-picker @change="dateChange"
                              v-model="saveForm.lectureTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择日期">
              </el-date-picker>
            </div>
          </template>
        </el-form-item>

        <el-form-item label="星期时间">
          <el-input v-model="saveForm.week = this.selectWeek" readonly></el-input>
        </el-form-item>

        <!--        <el-form-item label="星期时间">-->
        <!--          <el-input-->
        <!--              v-model="this.getWeek()"-->
        <!--              :disabled="true"/>-->
        <!--        </el-form-item>-->


        <el-form-item label="备注" prop="remark">
          <div class="edit_container">
            <quill-editor v-model="saveForm.remark"
                          :options="editorOption"
            >
            </quill-editor>
          </div>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>



//引入Vue的ueditor的资源
import {quillEditor} from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';

import {align} from "quill/ui/icons";


// 工具栏配置
const toolbarOptions = [
  ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
  ["blockquote", "code-block"], // 引用  代码块-----['blockquote', 'code-block']
  [{header: 1}, {header: 2}], // 1、2 级标题-----[{ header: 1 }, { header: 2 }]
  [{list: "ordered"}, {list: "bullet"}], // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
  [{script: "sub"}, {script: "super"}], // 上标/下标-----[{ script: 'sub' }, { script: 'super' }]
  [{indent: "-1"}, {indent: "+1"}], // 缩进-----[{ indent: '-1' }, { indent: '+1' }]
  [{direction: "rtl"}], // 文本方向-----[{'direction': 'rtl'}]
  [{size: ["small", false, "large", "huge"]}], // 字体大小-----[{ size: ['small', false, 'large', 'huge'] }]
  [{header: [1, 2, 3, 4, 5, 6, false]}], // 标题-----[{ header: [1, 2, 3, 4, 5, 6, false] }]
  [{color: []}, {background: []}], // 字体颜色、字体背景颜色-----[{ color: [] }, { background: [] }]
  [{font: []}], // 字体种类-----[{ font: [] }]
  [{align: []}], // 对齐方式-----[{ align: [] }]
  ["clean"], // 清除文本格式-----['clean']
  ["image", "link"] // 链接、图片、视频-----['link', 'image', 'video']
];


export default {
  //富文本
  components: {
    quillEditor
  },

  data() {
    return {
      //1.分页查询相关数据
      totals: 0,//总记录条数
      currentPage: 1,//当前页,要传递到后台的
      pageSize: 5, //每页显示多少条
      tableDatas: [], //当前页数据

      listLoading: false, //加载圈

      success:"",
      title: '',

      //2.高级查询相关数据
      keyword: '',

      //3.删除相关数据

      //4.批量删除相关数据
      sels: [], //存储多选记录

      selectWeek:'',

      exportFileAllIds:[],//全部导出id

      //5.添加和修改相关数据
      saveFormVisible: false,
      saveForm: {
        id: null,
        lectureTime: '',
        week: '',
        content: '',
        teacher: '',
        remark: '',
      },

      //6.添加和修改校验规则-表单项校验
      saveFormRules: {
        content: [
          {required: true, message: '请输入授课内容', trigger: 'blur'}
        ],
        teacher: [
          {required: true, message: '请输入授课讲师', trigger: 'blur'}
        ],
        remark: [
          {required: true, message: '请填写备注', trigger: 'blur'}
        ],
      },
      //富文本编辑器配置
      editorOption: {
        modules: {
          //工具栏定义的
          toolbar: toolbarOptions
        },
        //主题
        theme: "snow",
        placeholder: "请开始书写您的课程备注内容..."
      },

    }
  },
  methods: {

    //跨页操作
    SpreadPages(row){
      return row.id
    },
    //导出全部数据
    exportFileAll(){
       this.$http.get("/course").then(res=>{
        let exportFileAllIds = res.data.map(obj => obj.id)
         this.$confirm('此操作将导出全部数据, 是否继续?', '提示', {
           confirmButtonText: '确定',
           cancelButtonText: '取消',
           type: 'warning'
         }).then(() => {
           this.$http.patch("/course/exportFile",exportFileAllIds).then(res => {
             if (res.data.success) {
               this.$message.success("导出全部数据操作成功")
               this.getTableData()
             } else {
               this.$message.error("服务器异常，导出全部数据文件失败")
             }
           }).catch(res => {
             this.$message.error("网络异常，请查看您的网络")
           })
         }).catch(() => {
           this.$message.error("已取消操作")
         });
       })
    },
    //导出文件
    exportFile(){
      let ids = this.sels.map(obj => obj.id)//获取批量删除对象的id，组成数组。
      if (ids != ''){
        this.$http.patch("/course/exportFile",ids).then(res => {
          if (res.data.success) {
            this.$message.success("导出文件成功-d:/course.xlsx")
            this.getTableData()
          } else {
            this.$message.error("服务器异常，导出文件失败")
          }
        }).catch(res => {
          this.$message.error("网络异常，请查看您的网络")
        })
      }else {
        this.$message.error("你未选择导出的数据")
      }

    },
    //1.分页查询相关方法
    //1.1.获取列表数据
    getTableData() {
      this.listLoading = true //开启加载圈
      var params = {
        "currentPage": this.currentPage, //点击的当前第几页
        "pageSize": this.pageSize,   //当前页需要显示多少条数据
        "keyword": this.keyword    //模糊查询 ->关键字
      }
      this.$http.post("/course", params).then(request => {


        //接收参数添加到表格中(tableDatas),request.data ->固定写法
        this.tableDatas = request.data.list
        this.totals = request.data.total
        this.listLoading = false //关闭加载圈
      })
    },
    //1.2.选择第几页时触发
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage //1 -> 2
      this.getTableData()
    },
    //1.3.选择每页显示记录条数时触发
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.getTableData()
    },
    //2.高级查询相关方法
    keywordQuery() {
      this.currentPage = 1; //从第一页开始查询
      this.getTableData();
    },
    //3.删除相关方法
    //row -> 当前行对象 获取一行数据
    handleDel(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete("/course/" + row.id,).then(res => {
          if (res.data.success) {
            this.$message.success("操作删除成功")
            this.getTableData()
          } else {
            this.$message.error("服务器异常，删除失败")
          }
        }).catch(res => {
          this.$message.error("网络异常，请查看您的网络")
        })
      }).catch(() => {
        this.$message.error("已取消删除")
      });
    },

    //4.批量删除相关方法
    //4.1.选项改变时触发
    handleSelectionChange(val) {
      this.sels = val
    },
    //4.2.批量删除
    handlePatchDel() {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = this.sels.map(obj => obj.id)//获取批量删除对象的id，组成数组。
        this.$http.patch("/course", ids).then(res => {
          if (res.data.success) {
            this.$message.success("操作批量删除成功")
            this.getTableData()
          } else {
            this.$message.error("服务器异常，批量删除失败")
          }
        }).catch(res => {
          this.$message.error("网络异常，请查看您的网络")
        })
      }).catch(() => {
        this.$message.error("已取消批量删除")
      });
    },

    //5.添加和修改相关方法
    //5.1.点击添加弹出对话框
    handleAdd() {
      this.title = "添加数据"
      this.saveFormVisible = true //打开模态框
      this.saveForm = {
        id: null,
        lectureTime: '',
        content: '',
        teacher: '',
        remark: '',
      }
    },
    //5.2.点击修改弹出对话框
    handleEdit(row) {
      this.title = "编辑数据"
      this.saveFormVisible = true //打开模态框
      this.saveForm = Object.assign({}, row)
    },
    //5.3.提交表单数据操作数据库
    saveSubmit() {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$http.put("/course", this.saveForm).then(res => {
            if (res.data.success) {
              this.$message.success("操作" + this.title + "成功")
              this.getTableData()
              this.saveFormVisible = false
            } else {
              this.$message.error("服务器异常" + this.title + "未成功")
              this.saveFormVisible = false
            }
          }).catch(res => {
            this.$message.error("网络异常，请查看您的网络")
          })
        } else {
          this.$message.error("您有必须填写的数据未填写")
        }
      })

    },


    //点击日期自动回显星期日期，不能自己手动修改星期日期
    dateChange(value) {
      let date = new Date(value);
      let enumWeek = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六',]
      if (date) {
         this.selectWeek = enumWeek[date.getDay()]
      } else {
          this.selectWeek = null
      }
    },
  },

  mounted() {
    this.getTableData();
  }
}

</script>

<style scoped></style>
